package com.pan.crawler.impl2;

import com.pan.crawler.Page;
import com.pan.entity.Chapter;
import org.openqa.selenium.By;
import org.openqa.selenium.edge.EdgeDriver;

import java.time.Duration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PageImpl2 implements Page {

    private EdgeDriver edgeDriver;

    public PageImpl2(){
        edgeDriver = new EdgeDriver();
        edgeDriver.manage().window().minimize();
    }

    public String clear(String content){
        String[] delRegex = new String[]{
                "请退出转码页面，请下载爱阅小说app 阅读最新章节。\n" +
                        "\n" +
                        "八一中文网为你提供最快的[\\x{4e00}-\\x{9fa5}]+更新，[\\x{4e00}-\\x{9fa5}]+费阅读。https://www.81zw.com$",
                "网站即将关闭，下载爱阅app免费看最新内容\n"
        };
        for (int i = 0; i < delRegex.length; i++) {
            Pattern pattern = Pattern.compile(delRegex[i]);
            Matcher matcher = pattern.matcher(content);
            content = matcher.replaceAll("");
        }
        return content;
    }

    @Override
    public String get(Chapter chapter) {
        String url = chapter.getUrl();
        String content = "";
        for (int i = 0; i < 5; i++) {
            try {
                edgeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
                edgeDriver.navigate().to(url);
                content = edgeDriver.findElement(By.id("content")).getText();
                break;
            }catch (Exception e){
                if (i==4) throw new RuntimeException("请求页面失败");
            }
        }
        String title = chapter.getTitle();
        content ="\n\n\n\n\n\n\n"+title+"\n\n\n\n"+ clear(content);
        return content;
    }

    @Override
    public void close() {
        edgeDriver.quit();
    }
}
